home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Snippets / Stuart's Tech Notes / Stu’sThreadUtils / PipeLine.h < prev    next >
Encoding:
Text File  |  1994-03-19  |  1.5 KB  |  40 lines  |  [TEXT/KAHL]

  1. // Pipeline.h
  2. // 
  3. // (C) 6th March 1994  Stuart Cheshire <cheshire@cs.stanford.edu>
  4. // 
  5. // Any thread can read from a PipeLine.
  6. // A thread that wishes to write should first declare its intention by
  7. // calling PipeLineOpen, and should call PipeLineClose when it has finished.
  8. // A pipeline keeps track of how many writers it has, and if there are no
  9. // writers it returns error code -1 to any thread that tries to read from it,
  10. // to indicate that there is no more data.
  11. // If you wish to have readers block and wait for a future writer to come along
  12. // and provide data, instead of getting an error code back, then immediately after
  13. // initializing the pipeline with PipeLineInit, call PipeLineOpen. This will make
  14. // the pipeline think that there is always at least one active writer, so it will
  15. // never return the "no writers" indication.
  16.  
  17. #ifndef __PIPELINE__
  18. #define __PIPELINE__
  19.  
  20. #include "ThreadSynch.h"
  21.  
  22. #define NO_WRITERS (-1)
  23.  
  24. typedef struct
  25.     {
  26.     unsigned char *data;            // pointer to the data buffer
  27.     unsigned char *data_end;        // end data buffer
  28.     unsigned char *inptr, *outptr;    // pointers to where data is going in and out
  29.     int writers;                    // number of clients writing into this buffer
  30.     Semaphore mutex, chars, spaces;
  31.     } PipeLine;
  32.  
  33. extern void  PipeLineInit(PipeLine *p, unsigned char *buffer, unsigned long size);
  34. extern void  PipeLineOpen(PipeLine *p);
  35. extern void  PipeLineClose(PipeLine *p);
  36. extern void  PipeLinePutData(PipeLine *p, unsigned char x);
  37. extern short PipeLineGetData(PipeLine *p);
  38.  
  39. #endif
  40.